﻿

using System;
using System.Collections.Generic;

namespace LeetCodePractice.Topic
{
    public class T00001_TwoSum : ITestBase
    {
        public void Test()
        {
            int[] input1 = new int[] { 2, 7, 11, 15 }
            , input2 = new int[] { 3,2,4}
            , input3 = new int[] { 3, 3 }
            , input4 = new int[] { -3,4,3, 90 }
            , input5 = new int[] { 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 7, 1, 1, 1, 1, 1 }
            ;
            int target1 = 9, target2 = 6, target3 = 6,target4=0, target5 = 11;
            var action = (int[] input, int target) =>
            {
                var res = Sum(input, target);
                Console.WriteLine($"[{string.Join(",", input)}]=>[{string.Join(",", res)}]");
            };
            action(input1, target1);
            action(input2, target2);
            action(input3, target3);
            action(input4, target4);
            action(input5, target5);
        }
        public int[] Sum(int[] nums, int target)
        {
            int[] res = new int[2];
            System.Collections.Generic.Dictionary<int, int> diffs = new System.Collections.Generic.Dictionary<int, int>();
            for (int i = 0; i < nums.Length; i++)
            {
                int diff = target - nums[i];
                {
                    if (diffs.ContainsKey(nums[i]))
                    {
                        res[0] = diffs[nums[i]];
                        res[1] = i;
                        return res;
                    }
                    else
                    {
                        if (diffs.ContainsKey(diff) == false)
                        {
                            diffs.Add(diff, i);
                        }
                    }
                }
            }
            return res;
        }
    }
}
